package com.sch.www.ele.controller;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.sch.www.common.base.BaseController;
import com.sch.www.common.base.ResultBean;
import com.sch.www.common.validator.NotBlankFluentValidator;
import com.sch.www.ele.entity.CareMember;
import com.sch.www.ele.services.CareGroupServices;
import com.sch.www.ele.services.CareMemberServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("v1/careMember")
@RestController
public class CareMemberController extends BaseController {


    @Autowired
    CareMemberServices careMemberServices;

    @Autowired
    CareGroupServices careGroupServices;

    @RequestMapping(value = "listByGroupId")
    public ResultBean<List<CareMember>> listByGroupId(String groupId) {
        ResultBean<List<CareMember>> resultBean = ResultBean.newInstance();
        List<CareMember> page = careMemberServices.selectByGroupId(groupId);
        resultBean.setSuccess().setData(page);
        return resultBean;
    }


    @PostMapping("add")
    public ResultBean<CareMember> add(@RequestBody CareMember careMember) {
        ResultBean<CareMember> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(careMember.getGroupId(), new NotBlankFluentValidator("groupId"))
                .on(careMember.getUserId(), new NotBlankFluentValidator("userId"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        List<CareMember> careMemberList = careMemberServices.selectByGroupId(careMember.getGroupId());
        boolean exist = careMemberList.stream().anyMatch(item -> careMember.getUserId().equals(item.getUserId()));
        if (exist){
            resultBean.setError().setMsg("该组已存在该成员");
        }else{
            careMemberServices.insertSelective(careMember.getGroupId(), careMember.getUserId());
        }

        return resultBean;
    }


    @RequestMapping("removeById")
    public ResultBean<CareMember> remove(String id) {
        ResultBean<CareMember> resultBean = ResultBean.newInstance();
        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(id, new NotBlankFluentValidator("id"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            return resultBean.setError().appendMsg(complexResult);
        }
        careMemberServices.deleteById(id);
        return resultBean;
    }


}
